<template>
  <van-button
    v-if="value"
    class="follow-btn"
    round
    size="small"
    @click="onfllow"
    :loading = 'loading'
  >已关注</van-button>
  <van-button
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    @click="onfllow"
    :loading = 'loading'
  >关注</van-button>
</template>

<script>
import { addfollw, delfollw } from '@/api/user'
export default {
  name: 'fllowIndex',
  data () {
    return {
      loading: false
    }
  },
  props: {
    value: {
      type: Boolean,
      required: true
    },
    user_id: {
      type: [Number, String, Object],
      required: true
    }
  },
  methods: {
    async onfllow () {
      this.loading = true // 展示关注加载按钮的状态
      try {
        if (this.value) {
          // 已关注，取消关注
          await delfollw(this.user_id)
        } else {
          // 未关注， 添加关注
          await addfollw(this.user_id)
        }
        this.$emit('input', !this.value)
      } catch (err) {
        let message = '操作失败，请重试'
        if (err.response && err.response.status === 400) {
          message = '不能关注自己'
        }
        this.$toast.fail(message)
      }
      this.loading = false // 展示取消关注加载按钮的状态
    }
  }
}
</script>

<style scoped lang="less">

</style>
